﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace ReflectionStudio.LocalizationLibrary
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    //LocalizationLibrary References
    using LocalizationLibrary.Messaging;

    /// <summary>
    /// Defines static resouces' table.
    /// </summary>
    internal class StaticResourcesTable
    {
        //Fields

        private Dictionary<String, StaticResource> _registeredStaticResources;

        //Properties

        /// <summary>
        /// Gets registered static resources' dictionary.
        /// </summary>
        protected Dictionary<String, StaticResource> RegisteredStaticResources 
        {
            get 
            {
                if (this._registeredStaticResources == null)
                {
                    this._registeredStaticResources = new Dictionary<string, StaticResource>();
                }
                return this._registeredStaticResources;
            }
        }

        //Methods

        /// <summary>
        /// Adds the input <paramref name="statiResource"/> into <see cref="StaticResourceTable.RegisteredStaticResources"/>.
        /// </summary>
        /// <param name="staticResource">Static resource.</param>
        /// <returns>True whether adds the resource, False otherwise.</returns>
        public bool Add(StaticResource staticResource) 
        {
            bool retVal = false;
            String key = String.Format("{0}:{1}", staticResource.Key, staticResource.ResourceType.FullName);
            if ((staticResource != null) && (!this.RegisteredStaticResources.ContainsKey(key)))
            {
                this.RegisteredStaticResources.Add(key, staticResource);
                MessagingManager.Publish(new ResourceMessage(staticResource, Operation.Created));
                retVal = true;
            }
            return retVal;
        }

        /// <summary>
        /// Resets the <see cref="StaticResourceTable"/>.
        /// </summary>
        public void Reset()
        {
            lock (this.RegisteredStaticResources)
            {
                this.RegisteredStaticResources.Clear();
            }
        }
    }
}